﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoAnWebsite.Models;

namespace DoAnWebsite.Controllers
{
    public class ChanDoanController : Controller
    {
        private static DoAnWebsite_Entities db = new DoAnWebsite_Entities();
        //
        // GET: /ChanDoan/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Step1(Step1 model)
        {

            var listmodel = model.step1.Split(' ');
            var maxId = db.Commons.OrderByDescending(x => x.Id).FirstOrDefault();
            var n = maxId.Id + 1;
            var resultSearch = new int[n];
            var listStopWord = db.WordStops.ToList().Select(x => x.Word);
            foreach (var item in listmodel)
            {
                string item1 = item;
                bool contains = listStopWord.Contains(item1);
                if (!contains)
                {
                    var listtemp = db.Commons.Where(x => x.TrieuChung.Contains(item1));
                    foreach (var common in listtemp)
                    {
                        int i = common.Id;
                        resultSearch[i] += 1;
                    }
                }
            }

            int cmpItem = resultSearch.Max();
            var cmpList = new List<int>();
            if (cmpItem > 0)
            {
                for (int i = 0; i < n; i++)
                {
                    if (resultSearch[i] == cmpItem) cmpList.Add(i);
                }
                if (cmpList.Count < 3)
                {
                    for (int j = cmpItem - 1; (j > 0) && (cmpList.Count < 3); j--)
                    {
                        for (int i = 0; i < n && (cmpList.Count < 3); i++)
                        {
                            if (resultSearch[i] == j) cmpList.Add(i);
                        }
                    }
                }
            }
            var listTop3Result = cmpList.Select(item => db.Commons.SingleOrDefault(x => x.Id == item)).ToList();
            var listBenh = listTop3Result.Select(x => x.Benh);

            var listRole = new List<int>();
            foreach (var item in listBenh)
            {
                var item1 = item;
                var sublistGT = db.Roles.Where(x => x.Conclusion == item1).ToList();
                foreach (var gt in sublistGT)
                {
                    var temp = gt.Assumptions.Split(',');
                    foreach (int tt in temp.Select(int.Parse).Where(tt => !listRole.Contains(tt)))
                    {
                        listRole.Add(tt);
                    }
                }
            }
            Session["listRole"] = listRole;
            return RedirectToAction("Step2");
        }

        public ActionResult Step2()
        {
            var list = (List<int>)Session["listRole"];
            var listTT = new List<Symptom>();
            foreach (var item in list)
            {
                var temp = db.Symptoms.SingleOrDefault(x => x.Id == item);
                if (!listTT.Contains(temp))
                {
                    listTT.Add(temp);
                }
            }
            ViewBag.list = listTT;
            return PartialView("Step2");
        }

        [HttpPost]
        public ActionResult Step2(FormCollection form)
        {
            var lstValue = form.GetValues("chk");
            var list = new List<int>();
            if (lstValue != null)
            {
                foreach (var item in lstValue)
                {
                    var item1 = item.Replace("/", "");
                    list.Add(Int16.Parse(item1));
                }
            }

            var role = db.Roles;
            var listKQ = new List<KQModel>();
            foreach (var item in role)
            {
                var temp = item.Assumptions.Split(',');
                bool flag = temp.All(tt => list.Contains(Int16.Parse(tt)));
                if (flag)
                {
                    var kq = new KQModel();
                    Role item1 = item;
                    var benh = db.Benhs.SingleOrDefault(x => x.Id == item1.Conclusion);
                    kq.KetLuan = benh.Name;
                    kq.DoChacChan = 0.7 * item.Weight;
                    var test = listKQ.Find(x => kq.KetLuan.Equals(x.KetLuan));
                    if (test == null) listKQ.Add(kq);
                    else
                    {
                        test.DoChacChan = kq.DoChacChan + test.DoChacChan - kq.DoChacChan * test.DoChacChan;
                    }
                }
            }
            Session["listKQ"] = listKQ;
            return RedirectToAction("Step3");
        }

        public ActionResult Step3()
        {
            var input = (List<KQModel>)Session["listKQ"];
            if (input.Count == 0)
            {
                ViewBag.KQ = true;
                //ViewBag.KQ = "Xin lỗi, thông tin cung cấp chưa đủ để hệ thống chẩn đoán. Mời bạn thử lại!";
            }
            else
            {
                ViewBag.KQ = false;
            }
            ViewBag.input = Session["listKQ"];
            return PartialView("Step3");
        }

    }
}
